
/*

Design Pattern Automation Toolkit.
Application to create applications with emphasis on Design patterns.
And support for round trip engineering.
Copyright (C) 2004 Vineeth Neelakant. nvineeth@gmail.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

using System;
using BusinessLayer;
using InfoClasses;
using dpatoolkit.UILayer;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using DesignPatternDefinitions;

namespace dpatoolkit.UILayer
{
	/// <summary>
	/// Represents a UI for creates single object relation
	/// </summary>
	public class UIMultiCreates : IUIRelation
	{
		#region Fields
		private static  Pen scPen , ecPen ;
		#endregion 
		
		#region Constructor
		static UIMultiCreates()
		{
            scPen = new Pen(UIRenderer.MultiCreatesColor, UIRenderer.RelationPenSize);
            ecPen = new Pen(UIRenderer.MultiCreatesColor, UIRenderer.RelationPenSize);
			ecPen.CustomEndCap=PenCapProvider.GetMultipleRepresentation();
			ecPen.DashStyle = scPen.DashStyle = DashStyle.Dash;

		}
		
		public UIMultiCreates( IUIObject _o1, IUIObject _o2,IDesignPatternSubject sub):
		base ( String.Empty,_o1,_o2,null)
		{
			this.subject = sub;
			this.relationContextMenu.MenuItems[editIndex].Click +=new EventHandler(UIMultiCreatesEdit_Click);
			this.relationContextMenu.MenuItems[revIndex].Click +=new EventHandler(UIMultiCreatesRev_Click);
			this.relationContextMenu.MenuItems[delIndex].Click +=new EventHandler(UIMultiCreatesDel_Click);
            this.relationType = RelationType.MultipleCreate;
		}	

		#endregion

		#region IUIRelation Members

		
		public  override void Draw(Graphics g)
		{
			base.DrawLines(g,scPen,ecPen,scPen);
			
	
		}

		public  override void SetPariticapants(Rectangle r1, Rectangle r2)
		{
			
		}

		public override ContextMenu ContextMenu
		{
			get
			{
				return this.relationContextMenu;
			}
			set
			{
				//not implemented :P :-P
			}
		}




		#endregion

		private ICreation ConstructRelationObject()
		{
			ICreation cr = new CreationInfo();
			cr.CreatorClassName = this.obj1.UIName;
			cr.CreatorComponentName = this.obj1.UIComponent;
			cr.CreateeClassName = this.obj2.UIName;
			cr.CreateeComponentName = this.obj2.UIComponent;
			cr.Order = RelationOrder.Multiple;
			return cr;
		}
		private void UIMultiCreatesDel_Click(object sender, EventArgs e)
		{
			ICreation cr = this.ConstructRelationObject();
			this.UnRegisterObserver();
			this.subject.RemoveCreates(cr);
		}

		private void UIMultiCreatesEdit_Click(object sender, EventArgs e)
		{
			ICreation oldcr = this.ConstructRelationObject();
			ICreation newcr = this.ConstructRelationObject();
			RelationEditForm form = new RelationEditForm(null);
			form.Order = oldcr.Order;
			if( form.ShowDialog()== DialogResult.Cancel)
				return;
			newcr.Order = form.Order;
			if(oldcr.Order!=newcr.Order)
				this.subject.UpdateCreates(oldcr,newcr);

		}

		private void UIMultiCreatesRev_Click(object sender, EventArgs e)
		{
			ICreation oldcr = this.ConstructRelationObject();
			if(oldcr.CreatorComponentName!=oldcr.CreateeComponentName)
			{
				InfoMessage.Show("Sorry currently not supported when exported classes are present!");
				return;
			}

			ICreation newcr = this.ConstructRelationObject();
			newcr.CreatorClassName = oldcr.CreateeClassName;
			newcr.CreateeClassName = oldcr.CreatorClassName;
			newcr.CreatorComponentName = oldcr.CreateeComponentName;
			newcr.CreateeComponentName = oldcr.CreatorComponentName;
			this.subject.UpdateCreates(oldcr,newcr);
		}
	}
}

